#include "LocalAxisTransform.h"

LocalAxisTransform::LocalAxisTransform(void):m_Origin(0,0,0),m_X(1,0,0),m_Z(0,0,1),m_Y(0,1,0)
{
}

LocalAxisTransform::~LocalAxisTransform(void)
{
}

void LocalAxisTransform::SetAxis(DAEFloat3 origin, DAEFloat3 xAxis, DAEFloat3 yAxis, DAEFloat3 zAxis)
{
	m_Origin = origin;
	m_X = xAxis;
	m_Y = yAxis;
	m_Z = zAxis;
	m_Changed=true;
}

void LocalAxisTransform::SetCameraTransform(bool rightHanded, DAEFloat3 eye, DAEFloat3 target, DAEFloat3 up)
{
	m_Origin = eye;
	if ( rightHanded )
	{ 	
		eye.Subtract(m_Z,target);
		m_Z.Normalize();
	}
	else
	{
		target.Subtract(m_Z,eye);
		m_Z.Normalize();
	}
	up.CrossAndNormalize(m_X,m_Z);
	m_Z.CrossAndNormalize(m_Y,m_X);
	m_Changed=true;
}

void LocalAxisTransform::TransformLookAndUpVector(const Transform& t)
{
	t.TransformVector(m_X,m_X);
	t.TransformVector(m_Z,m_Z);
	Normalize();
	SetChanged(true);
}

void LocalAxisTransform::Normalize()
{
	// keep everything orthogonal & normalized.
	m_Z.Normalize();
	m_Z.CrossAndNormalize(m_Y,m_X);
	m_Y.CrossAndNormalize(m_X,m_Z);
}

void LocalAxisTransform::ResetTransformParameters()
{
	m_X.SetValues(1,0,0);
	m_Y.SetValues(0,1,0);
	m_Z.SetValues(0,0,1);
	m_Origin.SetValues(0,0,0);
	m_Changed = true;
}

void LocalAxisTransform::RecalculateMatrix() const
{
	m_CompiledMatrix.SetRow(0, m_X.x, m_Y.x, m_Z.x,0);
	m_CompiledMatrix.SetRow(1, m_X.y, m_Y.y, m_Z.y,0);
	m_CompiledMatrix.SetRow(2, m_X.z, m_Y.z, m_Z.z,0);
	m_CompiledMatrix.SetRow(3, -m_Origin.Dot(m_X), -m_Origin.Dot(m_Y),-m_Origin.Dot(m_Z),1);
}

